package com.ruoyi.project.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.TreeNode;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.ProjectRecovery;
import com.ruoyi.project.service.IProjectRecoveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工程复盘目录Controller
 * 
 * @author ruoyi
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/project/recovery")
public class ProjectRecoveryController extends BaseController
{
    @Autowired
    private IProjectRecoveryService projectRecoveryService;

    /**
     * 查询工程复盘目录列表
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectRecovery projectRecovery)
    {
        startPage();
        List<ProjectRecovery> list = projectRecoveryService.selectProjectRecoveryList(projectRecovery);
        return getDataTable(list);
    }

    /**
     * 导出工程复盘目录列表
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:export')")
    @Log(title = "工程复盘目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectRecovery projectRecovery)
    {
        List<ProjectRecovery> list = projectRecoveryService.selectProjectRecoveryList(projectRecovery);
        ExcelUtil<ProjectRecovery> util = new ExcelUtil<ProjectRecovery>(ProjectRecovery.class);
        util.exportExcel(response, list, "工程复盘目录数据");
    }

    /**
     * 获取工程复盘目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:query')")
    @GetMapping(value = "/{recoveryId}")
    public AjaxResult getInfo(@PathVariable("recoveryId") Long recoveryId)
    {
        return success(projectRecoveryService.selectProjectRecoveryByRecoveryId(recoveryId));
    }

    /**
     * 新增工程复盘目录
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:add')")
    @Log(title = "工程复盘目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProjectRecovery projectRecovery)
    {
        System.out.println("projectRecovery = " + projectRecovery);
        /** 目录名称唯一验证 */
        Long id = projectRecoveryService.selectNameByProjectRecovery(projectRecovery);
        System.out.println("id = " + id);
        if(id != null){
            return error("目录名称不能重复!");
        }
        return toAjax(projectRecoveryService.insertProjectRecovery(projectRecovery));
    }

    /**
     * 修改工程复盘目录
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:edit')")
    @Log(title = "工程复盘目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProjectRecovery projectRecovery)
    {
        /** 目录名称唯一验证 */
        Long id = projectRecoveryService.selectNameByProjectRecovery(projectRecovery);
        if(id != null){
            return error("目录名称不能重复!");
        }
        return toAjax(projectRecoveryService.updateProjectRecovery(projectRecovery));
    }

    /**
     * 删除工程复盘目录
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:remove')")
    @Log(title = "工程复盘目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recoveryIds}")
    public AjaxResult remove(@PathVariable Long[] recoveryIds)
    {
        //查询是否有子节点,判断这个目录下是否有工程复盘内容
        Long id = projectRecoveryService.getIdByPid(recoveryIds[0]);
        if(id!=null){
            return error("请先删除子节点,再删除本节点!!~");
        }
        Long id1 = projectRecoveryService.getIdByProjectRecoveryDetail(recoveryIds[0]);
        if(id1!=null){
            return error("有约束节点不能删除!!~");
        }
        return toAjax(projectRecoveryService.deleteProjectRecoveryByRecoveryIds(recoveryIds));
    }

    /**
     * 树结构
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:query')")
    @GetMapping("/getTree/{projectId}")
    public AjaxResult getTree(@PathVariable("projectId") Long projectId)
    {
        TreeNode root = projectRecoveryService.getTree(projectId);
        System.out.println("root **************** " + root);
        return success(root) ;
    }
}
